import BMapInfoBox from './BMapInfoBox.js';
import './BMapInfoBox.less';

export function openInfoWindow(map, content, options) {
  // console.log(LngLat, infoWind);
  // infoWind.open(LngLat);
  return new BMapInfoBox(map, content, options);
}

export function openNormalInfoWindow(map, data, options) {
  // console.log(LngLat, infoWind);
  // infoWind.open(LngLat);
  const { name, content } = data;
  return openInfoWindow(
    map,
    `<div style="width: 230px;padding: 0.25em 1em;color: #21c67e">${name}</div>` +
      (content
        ? `<div style="width: 270px;padding: 0.5em 1em;line-height: 1.5;color: #c66621;letter-spacing: 0">${
            content || '暂无内容'
          }</div>`
        : ''),
    options
  );
}
export function openListInfoWindow({ map, data, getTitle, getContent, options, onSelect }) {
  if (!Array.isArray(data) || !data.length) {
    return;
  }
  const wrap = document.createElement('div');
  wrap.onwheel = function (e) {
    e.stopPropagation();
  };
  // wrap.style.cssText = 'max-height: 300px;overflow: hidden auto;';
  const title = typeof getTitle === 'function' ? getTitle(data) : '';
  if (title) {
    const titleEl = document.createElement('div');
    titleEl.style.cssText = 'padding: 0.25em 0.5em;font-size: 1.2em;color: #333';
    titleEl.innerHTML = title;
    wrap.appendChild(titleEl);
  }
  const contentEl = data.reduce((contentWrap, item) => {
    const div = document.createElement('div');
    div.style.cssText =
      'min-width: 160px;max-width: 500px;padding: 0.25em 1em;color: #21c67e;overflow: hidden;white-space: nowrap;text-overflow: ellipsis';
    div.innerHTML = typeof getContent === 'function' ? getContent(item) : item.name;
    div.onclick = (e) => {
      e.stopPropagation();
      e.preventDefault();
      typeof onSelect === 'function' && onSelect(item);
    };
    contentWrap.appendChild(div);
    return contentWrap;
  }, document.createElement('div'));
  contentEl.style.cssText = 'max-height: 300px;overflow: hidden auto;';
  wrap.appendChild(contentEl);

  return openInfoWindow(map, wrap, options);
}
